(function () {
    var addOrUpdate = {
        bindEvent: function () {
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
			/**
			 * 保存按钮点击事件
			 */
            $(".industry-kept").unbind().on("click", function () {
            	var textFieldValue = $("#textField").val();
            	if(textFieldValue == ""){
            		parent.layer.msg("产品名称不能为空");
            		return false;
            	}
                var modeSwitch = window.sessionStorage.getItem("modeSwitch");
                if (modeSwitch == 1) {//编辑
                    var editParamJSONString = window.sessionStorage.getItem("editParam");
                    var editParamDic = JSON.parse(editParamJSONString);
                    //var modifiedName = $("#textField").val();
                    var display_status = $("#selection option:selected").attr("displayStatus");
                    addOrUpdate.api_modifyShellProductWithNameAndPkid(textFieldValue, editParamDic["currentEditPkid"], display_status);
                } else if (modeSwitch == 2) {//新增
                    var display_status = $("#selection option:selected").attr("displayStatus");
                    addOrUpdate.api_insertShellProductWithName(textFieldValue, display_status);
                }
            });
			/**
			 * 取消按钮绑定事件
			 */
            $(".industry-cancel").unbind().on("click", function () {
                window.sessionStorage.setItem("modeSwitch", 0);
                parent.layer.close(index);
            });
			/**
			 * 关闭按钮绑定事件
			 */
            $("#close-window").unbind().on("click", function () {
                window.sessionStorage.setItem("modeSwitch", 0);
                parent.layer.close(index);
            });
        },
        init: function () {
            var modeSwitch = window.sessionStorage.getItem("modeSwitch");
            if (modeSwitch == 1) {//编辑
                var editParamJSONString = window.sessionStorage.getItem("editParam");
                var editParamDic = JSON.parse(editParamJSONString);
                $("#textField").val(editParamDic["currentProductName"]);
                $("#selection").val(editParamDic["currentProductStatus"]);
            } else if (modeSwitch == 2) {//新增
                
            }
        },
		/**
         * API请求--增加
         */
        api_insertShellProductWithName: function (keyword,displayStatus) {
            var url = "/shellProduct/insert";
            var functionParam = {
                "pk_user": window.localStorage.userid,
                "name": keyword,
                "enable": displayStatus,
            };
            common.interfaceAjaxFunction(url, functionParam, this.insertShellProductSuccess, true);
            common.showLoading();
        },
		/**
         * API请求--修改
         */
        api_modifyShellProductWithNameAndPkid: function (productName, primeryKey, displayStatus) {
            var url = "/shellProduct/update";
            //原来的名称
            var editParamJSONString = window.sessionStorage.getItem("editParam");
            var editParamDic = JSON.parse(editParamJSONString);
            //参数
            var functionParam = {
                "pk_user": window.localStorage.userid,
                "name": productName,
                "enable": displayStatus,
                "pk": primeryKey,
                "oldkey":editParamDic["currentProductName"]
            };
            common.interfaceAjaxFunction(url, functionParam, this.modifyShellProductSuccess, true);
            common.showLoading();
        },
		/**
         * 增加接口回调
         */
        insertShellProductSuccess: function (res) {
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            common.hideLoading();
            if (res.status != 0) {
                parent.layer.msg(res.message);
            } else {
                //调用父页面的查询
                window.sessionStorage.setItem("modeSwitch", 0);
                parent.$(".competion-search-btn").click();
                parent.layer.msg("新增成功");
                parent.layer.close(index);
            }
        },
		/**
         * 修改接口回调
         */
        modifyShellProductSuccess: function (res) {
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            common.hideLoading();
            if (res.status != 0) {
                parent.layer.msg(res.message);
            } else {
                window.sessionStorage.setItem("modeSwitch", 0);
                //调用父页面的查询
                parent.$(".competion-search-btn").click();
                parent.layer.msg("修改成功");
                parent.layer.close(index);
            }
        }
    };
    $(document).ready(function () {
        addOrUpdate.init();
        addOrUpdate.bindEvent();
    });
})();